// pages/my/my.js
const app = getApp()
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    wishList:{},
    active: 0,
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onlord')
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    if (this.data.userInfo) {
      this.setData({
        userInfo: this.data.userInfo,
      })
    } 
   
  },

  onChange(event) {
    console.log(event.detail)
    this.setData({
      active: event.detail.index
    });
    
    //获取标签页数据数据
    if (this.data.active == 1) {
      db.collection('Issues').where({
        _openid:this.data.userInfo.openid
      })
        .get({
          success: res => {
            this.setData({
              list: res.data
            })
          }
        });
    } else {
        this.setData({
          list: app.globalData.wishList
        })
    }
  },

  toDetail(e) {
    console.log(e)
    const issueId = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/detail/detail?issueId=' + issueId
    })
  },

  deleteWish(e) {
    const { item } = e.currentTarget.dataset
    const i = app.globalData.wishList.findIndex(v => v._id == item._id)
    console.log(i)
    app.globalData.wishList.splice(i,1)
    this.setData({
      list:app.globalData.wishList
    })
    wx.setStorageSync('wishList', app.globalData.wishList)
    wx.showToast({
      title: '删除成功',
      icon: 'success',
    })
  },

  deleteIssue(e){
    const issueId = e.currentTarget.dataset.item._id
    console.log(issueId)
    db.collection('Issues').where({
      _id: issueId
    })
      .remove({
        success: res => {
          wx.showToast({
            title: '删除成功',
            icon: 'success',
          })
          db.collection('Issues').where({
            _openid: this.data.userInfo.openid
          })
            .get({
              success: res => {
                this.setData({
                  list: res.data
                })
              }
            });
        }
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('onready')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onshow')
    this.setData({
      wishList: wx.getStorageSync('wishList')
    })
    if (this.data.active == 1) {
      db.collection('Issues').where({
        _openid: this.data.userInfo.openid
      })
        .get({
          success: res => {
            this.setData({
              list: res.data
            })
          }
        });
    } else {
      this.setData({
        list: this.data.wishList
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})